﻿#include "EditorMeshMaterialPropertyView.h"

#include "Widgets/TextWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/ImageWidget.h"
#include "Widgets/Positioners/VerticalPositionerWidget.h"
#include "Widgets/Keyboard/KeyboardWidget.h"
#include "Widgets/Mouse/MouseWidget.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Asset/EditorDefaultPackage.h"

#include "Window/EditorWindowGroupView.h"
#include "Widgets/EditorAssetSelectorWidget.h"
#include "Material/Material.h"

void REditorMeshMaterialPropertyView::BindViewModel(TSharedObjectPtr<REditorPropertyViewModel> InViewModel)
{
    Super::BindViewModel(InViewModel);

    StaticMeshViewModel=InViewModel;
}

void REditorMeshMaterialPropertyView::CreateWidget(TSharedObjectPtr<RVerticalPositionerWidget> InPropertyList)
{
    
    //create widget to set mesh default material
    auto Root= NewWidget<RWidget>(InPropertyList);
    Root->Anchor.HorizontalFill(InPropertyList);
    RWidgetWeakPtr RootWeak=Root;

    Root->Height=StaticMeshViewModel->LineHeight*2 * StaticMeshViewModel->GetMaterialSlotNum();

    auto NameList=NewWidget<RVerticalPositionerWidget>(Root);
    NameList->Anchor.LeftReference=Root;
    NameList->Width=[this](){
        return StaticMeshViewModel->GetSpliterLeftOffset();
    };
    NameList->Anchor.VerticalFill(Root);
    NameList->Spacing=0;

    auto Spliter=NewWidget<RRectangleWidget>(Root);
    Spliter->Width=StaticMeshViewModel->SpliterWidth;
    Spliter->X=StaticMeshViewModel->GetSpliterLeftOffset();
    Spliter->Anchor.VerticalFill(Root);
    Spliter->FillColor=REditor::Get()->CurrentTheme->SpliterColor;

    auto ValueList=NewWidget<RVerticalPositionerWidget>(Root);
    ValueList->Anchor.LeftReference=Spliter;
    ValueList->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    ValueList->Anchor.RightReference=Root;
    ValueList->Anchor.VerticalFill(Root);
    ValueList->Spacing=0;

    for(int64_t SlotIndex=0;SlotIndex<StaticMeshViewModel->GetMaterialSlotNum();SlotIndex++)
    {
        auto Name=NewWidget<RTextWidget>(NameList);
        Name->Text=U"材质"+CString(SlotIndex);
        Name->VerticalAlignment=ETextVerticalAlignment::Center;
        Name->Height=StaticMeshViewModel->LineHeight*2;

        auto ValueLine=NewWidget<RWidget>(ValueList);
        ValueLine->Height=StaticMeshViewModel->LineHeight*2;
        ValueLine->Anchor.HorizontalFill(ValueList);


        auto MaterialThumbnail=NewWidget<RImageWidget>(ValueLine);
        MaterialThumbnail->Width=StaticMeshViewModel->LineHeight*2;
        MaterialThumbnail->Height=StaticMeshViewModel->LineHeight*2;
        MaterialThumbnail->Image=StaticMeshViewModel->GetMaterialThumbnail(SlotIndex);

        const float PopUpIconSize=20;
        auto TextBackground=NewWidget<RRectangleWidget>(ValueLine);
        RRectangleWidgetWeakPtr WeakTextBackground=TextBackground;
        TextBackground->FillColor=REditor::Get()->CurrentTheme->TextInputBackgroundColor;
        TextBackground->Anchor.LeftReference=MaterialThumbnail;
        TextBackground->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
        TextBackground->Anchor.RightReference=ValueLine;
        TextBackground->Height=StaticMeshViewModel->LineHeight;

        auto MaterialName=NewWidget<RTextWidget>(TextBackground);
        MaterialName->Text=[this,SlotIndex]()
        {
            return StaticMeshViewModel->GetMaterialName(SlotIndex);
        };
        MaterialName->VerticalAlignment=ETextVerticalAlignment::Center;
        MaterialName->Anchor.VerticalFill(TextBackground);
        MaterialName->Anchor.RightReference=ValueLine;
        MaterialName->Anchor.RightOffset=PopUpIconSize;

        auto PopUpIcon=NewWidget<RImageWidget>(ValueLine);
        PopUpIcon->Width=PopUpIconSize;
        PopUpIcon->Height=PopUpIconSize;
        PopUpIcon->Image=REditorDefaultPackage::Get()->GetIcon(U"CodiconChevronDown");
        PopUpIcon->Anchor.RightReference=ValueLine;
    
        auto PopupKeyboard= NewWidget<RKeyboardWidget>(TextBackground);
        RKeyboardWidgetWeakPtr WeakPopupKeyboard= PopupKeyboard;

        auto ComboBoxMouse= NewWidget<RMouseWidget>(TextBackground);
        ComboBoxMouse->Anchor.Fill(TextBackground);
        ComboBoxMouse->AutoFocusTarget= PopupKeyboard;

        PopupKeyboard->OnKeyboardUnfocused.Bind([WeakPopupKeyboard]()
        {
            REditorWindowGroupView::Get()->ClosePopupMenu();
        });

        PopupKeyboard->OnKeyboardFocused.Bind([
            this
            ,WeakPopupKeyboard
            ,MaterialName
            ,SlotIndex
            ,WeakTextBackground
            ]()
        {
           auto ComboBoxPopup= REditorWindowGroupView::Get()
                ->CreatePopupMenu(
                    SIntPoint(StaticMeshViewModel->GetPopupMenuWidth(),StaticMeshViewModel->GetPopupMenuHeight())
                    ,WeakTextBackground
            );
            WeakPopupKeyboard->Anchor.Fill(ComboBoxPopup);

            auto PopupBackground= NewWidget<RRectangleWidget>(ComboBoxPopup);
            PopupBackground->Anchor.Fill(ComboBoxPopup);
            PopupBackground->FillColor= REditor::Get()->CurrentTheme->BackgroundColor;
            PopupBackground->Radius=4;
            PopupBackground->BorderSize=1;
            PopupBackground->BorderColor= REditor::Get()->CurrentTheme->SpliterColor;

            auto Selector=NewWidget<REditorAssetSelectorWidget>(ComboBoxPopup);
            Selector->Anchor.Fill(ComboBoxPopup);
            Selector->OnAssetSelected.Bind([this,WeakPopupKeyboard,SlotIndex](TSharedObjectPtr<RObject> InMaterial)
            {                
                StaticMeshViewModel->SelectAsset(InMaterial,SlotIndex);
                WeakPopupKeyboard->Unfocus();
            });
            Selector->SetAssetType(EEditorSelectableAssetType::Material);

        });

        
    }

}
